package com.wmh.baseservice.common.redis.serializer;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * key 统一前缀序列化器
 * @author 审心
 */
@RequiredArgsConstructor
public class KeyPrefixRedisSerializer implements RedisSerializer<String> {

    private final CacheProperties cacheProperties;

    @Override
    public String deserialize(byte[] bytes) {
        return (bytes == null ? null : new String(bytes).replaceFirst(cacheProperties.getRedis().getKeyPrefix(), ""));
    }

    @Override
    public byte[] serialize( String string) {
        return (string == null ? null : (cacheProperties.getRedis().getKeyPrefix() + string).getBytes());
    }
}
